<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\model;

use think\facade\Cache;
use think\Model;

/**
 * 角色模型
 * @package app\admin\model
 */
class Airport extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'airport';

    function addAll($list,$updater){
        self::startTrans();
        $rep=[];
        try{
            foreach ($list as &$val){
                if(empty($val['name'])
                        || empty($val['iata'])
                        || empty($val['terminal'])
                        || empty($val['country'])
                        || empty($val['city'])
                        || empty($val['longitude'])
                        || empty($val['latitude'])
                ){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='excel file data is empty!';
                    return $rep;
                }

                $country=Continent::where(['name'=>$val['country'],'type'=>'country'])->field('parent_id,id')->find()->toArray();
                if(empty($country)){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='country name is non-existent!';
                    return $rep;
                }
                $val['continent']=$country['parent_id'];
                $val['country']=$country['id'];
                $city=Continent::where(['name'=>$val['city'],'type'=>'city'])->value('id');
                if(empty($city)){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='city name '.$val['city'].' is non-existent!';
                    return $rep;
                }
                $val['city']=$city;
                $count=self::where(['name'=>$val['name']])->count();
                if($count){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='airport name is repeat!';
                    return $rep;
                }
                $val['terminal']=json_encode(explode(',',$val['terminal']));
                $val['update_user']=$updater;
                $val['update_time']=date('Y-m-d H:i:s');
                $val['create_time']=date('Y-m-d H:i:s');
                unset($val['parent_name']);
                $val['id']=self::insertGetId($val);
                if(!$val['id']){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='Server Internal Error!';
                    return $rep;
                }

            }
            self::commit();
            //添加位置信息到redis
            $handler=Cache::store('redis')->handler();
            foreach ($list as &$val){
                if($val['latitude'] && $val['longitude']){
                    $handler->geoadd(config('redis.redis_key.airport_location'),$val['longitude'],$val['latitude'],'airport_'.$val['id']);
                }
            }
            $rep['code']=1;
            return $rep;
        }catch (\Exception $e){
            var_dump($e->getMessage());
            self::rollback();
            $rep['code']=-1;
            $rep['msg']='Server Internal Error!';
            return $rep;
        }
    }

}
